class PlanController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [planInstanceList: Plan.list(params), planInstanceTotal: Plan.count()]
    }

    def create = {
        def planInstance = new Plan()
        planInstance.properties = params
        return [planInstance: planInstance]
    }

    

    def save = {
        def courseAndTeacher = params.courseAndTeacher
        def aCourseAndTeacher = courseAndTeacher.tokenize('|')
        
        aCourseAndTeacher.each{
             def aCourseTeacher = it.tokenize(',')
             
             if(aCourseTeacher[0] != "" && aCourseTeacher[1] !="" && aCourseTeacher[2] !=""){
                 def planInstance = Plan.get(aCourseTeacher[0].toLong())
                 if(planInstance){
                     Teacher t = Teacher.get(aCourseTeacher[1].toLong())
                     if(t){
                           planInstance.teacher = t
                           planInstance.course = aCourseTeacher[2]
                           planInstance.save(flush:true) 
                     }
                 }
             }      
        }
        def classes = Classes.get(params.classesid)
        TeacherCourseClasses.findAllByClasses(classes).each{
            it.delete(flush:true)
        }
        def teacherList = Plan.findAllByClasses(classes).grep{
            it.teacher
        }
        if(teacherList.size() > 0){
            teacherList.each{
                TeacherCourseClasses teachercc = new TeacherCourseClasses()
                teachercc.course = it.course
                teachercc.teacher = it.teacher
                teachercc.classes = it.classes
                teachercc.save()
            }
        }
        
        flash.message = "保存课表成功！"
        redirect(action:"planList",params:[classesid:params.classesid])
    }

     

    def show = {
        def planInstance = Plan.get(params.id)
        if (!planInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'plan.label', default: 'Plan'), params.id])}"
            redirect(action: "list")
        }
        else {
            [planInstance: planInstance]
        }
    }

    def edit = {
        def planInstance = Plan.get(params.id)
        if (!planInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'plan.label', default: 'Plan'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [planInstance: planInstance]
        }
    }

    def update = {
        def planInstance = Plan.get(params.id)
        if (planInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (planInstance.version > version) {
                    
                    planInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'plan.label', default: 'Plan')] as Object[], "Another user has updated this Plan while you were editing")
                    render(view: "edit", model: [planInstance: planInstance])
                    return
                }
            }
            planInstance.properties = params
            if (!planInstance.hasErrors() && planInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'plan.label', default: 'Plan'), planInstance])}"
                redirect(action: "list")
            }
            else {
                render(view: "edit", model: [planInstance: planInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'plan.label', default: 'Plan'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def planInstance = Plan.get(params.id)
        if (planInstance) {
            try {
                planInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'plan.label', default: 'Plan'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'plan.label', default: 'Plan'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'plan.label', default: 'Plan'), params.id])}"
            redirect(action: "list")
        }
    }

   
}
